<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="demoTable" style="margin-top: 20px;margin-bottom: 20px">
    <label>摊位编号</label>
    <div class="layui-inline" style="margin-right: 20px">
        <input class="layui-input" name="queryByNumber" id="queryByNumber" autocomplete="off" placeholder="请输入摊位编号">
    </div>
    <label>租用状态</label>
    <div class="layui-inline" style="margin-right: 20px">
        <div class="layui-form" lay-filter="myDiv">
            <select name="queryByStatus" id="queryByStatus" lay-filter="mySelect">
                <option value="">请选择</option>
                <option value="1">已租用</option>
                <option value="2">未租用</option>
                <option value="3">暂不可用</option>
            </select>
        </div>
    </div>
    <button class="layui-btn" id="queryButton">查询</button>
    <button class="layui-btn" data-type="reload" id="resetButton">重置</button>
    <button class="layui-btn" id="addButton">新增</button>
</div>

<!--  这是表格-->
<table class="layui-hide" id="LAY_table_manager" lay-filter="LAY_table_manager"></table>

<!--  点击新增弹出的页面-->
<!--  class="show-display-none"-->
<div hidden class="layui-form-pane" id="addModel" style="text-align:center">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>摊位新增</legend>
    </fieldset>

    <form class="layui-form" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block">
                    <input type="hidden" id="managerId" name="managerId" class="layui-input">
                    <input type="text" id="number" name="number" lay-verify="required" placeholder="数字编号" autocomplete="off" class="layui-input">
                </div>
                <!--                  <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>-->
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">方位</label>
                <div class="layui-input-inline">
                    <select name="site" id="site" lay-filter="mySelect">
                        <option value="1">东侧</option>
                        <option value="2">西侧</option>
                        <option value="3">南侧</option>
                        <option value="4">北侧</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">占地大小</label>
                <div class="layui-input-inline">
                    <input type="text" id="size" name="size" lay-verify="required" placeholder="单位：平方米" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-inline">
                    <input type="text" id="price" name="price" lay-verify="required" placeholder="单位：元/月（两位小数）" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submitForAdd">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

</div>


<!--  点击修改弹出的页面-->
<!--  class="show-display-none"-->
<div hidden class="layui-form-pane" id="editModel" style="text-align:center">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>摊位修改</legend>
    </fieldset>

    <form class="layui-form" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block">
                    <input type="hidden" id="boothIdForEdit" name="boothIdForEdit" class="layui-input">
                    <input type="text" id="numberForEdit" name="numberForEdit" lay-verify="required" placeholder="数字编号" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="statusForEdit" id="statusForEdit" lay-filter="mySelect2">
                        <option value="1">已租用</option>
                        <option value="2">未租用</option>
                        <option value="3">暂不可用</option>
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">方位</label>
                <div class="layui-input-inline">
                    <select name="siteForEdit" id="siteForEdit" lay-filter="mySelect">
                        <option value="1">东侧</option>
                        <option value="2">西侧</option>
                        <option value="3">南侧</option>
                        <option value="4">北侧</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">占地大小</label>
                <div class="layui-input-inline">
                    <input type="text" id="sizeForEdit" name="sizeForEdit" lay-verify="required" placeholder="单位：平方米" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-inline">
                    <input type="text" id="priceForEdit" name="priceForEdit" lay-verify="required" placeholder="单位：元/月（两位小数）" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submitForEdit">确认修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

</div>



<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../js/jquery.min.js" charset="utf-8"></script>
<script src="../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','layer','laydate','upload'], function(){
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            uplode = layui.upload;

        //加载表格
        table.render({
            elem: '#LAY_table_manager'
            ,url:'http://localhost:8081/booth/api/managerQueryBooth'
            ,where:{
                managerId:'21fgg345344'
            }
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,id: 'testReload'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'number', title:'编号', width:120}
                // ,{field:'id', title:'ID', width:300, fixed: 'left', unresize: true, sort: true}
                ,{field:'marketName', title:'所属夜市', width:200, edit: 'text'}
                ,{field:'siteValue', title:'位置', width:120, edit: 'text'}
                ,{field:'size', title:'占地大小 /m²', width:180, sort: true, edit: 'text'}
                ,{field:'price', title:'价格 /元', width:180, sort: true}
                ,{field:'statusTs', title:'租用状态', width:180}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true
        });

        //查询事件
        $('#queryButton').click(function () {
            table.reload('LAY_table_manager',{
                page:{
                    curr:1
                },
                where:{
                    number: $('#queryByNumber').val(),
                    status: $('#queryByStatus').val()
                }
            });
        })

        //  重置事件
        $('#resetButton').click(function () {
            $('#queryByNumber').val('');
            $('#queryByStatus').val()
        })

        //新增事件
        $('#addButton').click(function () {
            //调出新增弹窗
            addPanel()
        })

        //修改和删除事件
        table.on('tool(LAY_table_manager)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'del'){
                layer.confirm('真的删除该记录么？', function(index){
                    deluser(data,index,obj);
                } );
            }
            if(layEvent === 'edit'){
                layer.confirm('真的修改该记录么？', function(index){
                    editPanel(data,index,obj);
                } );
            }
        });

        function addPanel() {
            $('#managerId').val('21fgg345344');
            $('#number').val('');
            $('#site').val('');
            $('#size').val('');
            $('#price').val('');
            layer.open({
                type: 1,
                title: '摊位新增',
                area: ["30%","70%"],  //宽 高
                content: $('#addModel') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        }

        function editPanel(data,index,obj) {
            //先把值返现给对应的文本框，然后用组件把弹窗给整出来
            $('#boothIdForEdit').val(data.id);
            $('#numberForEdit').val(data.number);
            $('#statusForEdit').val(data.status);
            $('#siteForEdit').attr("value",data.site);
            $('#sizeForEdit').val(data.size);
            $('#priceForEdit').val(data.price);


            layer.open({
                type: 1,
                title: '用户修改',
                area: ["40%","60%"],  //宽 高
                content: $('#editModel') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        }

        //提交新增表单
        form.on('submit(submitForAdd)', function(data){
            // window.alert(JSON.stringify(data.field));
            $.ajax({
                url: "http://localhost:8081/booth/api/addBooth",    //这个是后台的路由地址
                type: "POST",
                data : {
                    'managerId' : data.field.managerId,
                    'number' : data.field.number,
                    'site' : data.field.site,
                    'size' : data.field.size,
                    'price' : data.field.price
                },
                dataType: "json",     //----可能必须是json
                success: function (data) {
                    //同步更新表格和缓存对应的值
                    layer.msg("成功", {icon: 6});
                },
                error : function(e){
                    layer.msg("失败", {icon: 6});
                }
            });
        });

        //提交修改表单
        form.on('submit(submitForEdit)', function(data){
            //console.log(data)
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            var data1 ={
                'id': data.field.boothIdForEdit,
                'number' : data.field.numberForEdit,
                'status' : data.field.statusForEdit,
                'site': data.field.siteForEdit,
                'size' : data.field.sizeForEdit,
                'price' : data.field.priceForEdit
            }
            $.ajax({
                url: "http://localhost:8081/booth/api/updateBooth",    //这个是后台的路由地址
                type: "POST",
                data : data1,
                dataType: "json",     //----可能必须是json
                success: function (data) {
                    //同步更新表格和缓存对应的值

                    layer.msg("修改成功", {icon: 6});
                },
                error : function(e){
                    layer.msg("修改失败", {icon: 5});
                }
            });
        });

        //删除按钮调用的方法
        function deluser(data,index,obj) {
            $.ajax({
                url: "http://localhost:8081/booth/api/deleteBooth",    //这个是后台的路由地址
                type: "POST",
                data: {"id": data.id},//传给后台的值
                dataType: "json",     //----可能必须是json
                success: function (data) {
                    //同步更新表格和缓存对应的值
                    obj.del();
                    layer.msg("删除成功", {icon: 6});
                },
                error : function(e){
                    layer.msg("删除失败", {icon: 5});
                }
            });
        }

    });
</script>

</body>
</html>